<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class BackStaffService extends ServiceBase {


    public function getList(){
        //允许排序的字段名称数组
        $canOrderCols = array('name', 'updated_at');

        $order = Input::get( 'order' );
        $reverse = Input::get( 'reverse',0 );
        $page = Input::get( 'page', 1 );

        $entries = Staff::orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        $entries = $entries->paginate(Config::get('app.pagenate_num'))
            ->addQuery('order',$order)
            ->addQuery('reverse',$reverse);

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
        );

        return array('entries'=>$entries, 'links'=>$links);

    }

    public function createStaff(){
        $staff = new Staff();
        $this->parseInput($staff,'name');

        if(!$staff->validate()){
            $this->errors()->merge($staff->errors());
            return false;
        }
        //create user
        $repo = App::make('UserRepository');
        $user = $repo->signup(Input::all());

        if (!$user->id) {
            $this->errors()->merge($user->errors());
            return false;
        }
        //add roles
        $user->saveRoles(Input::get( 'roles' ));
        //create staff
        if(!$staff->save()){
            $this->errors()->merge($staff->errors());
            return false;
        }
        //relate user and staff
        $user->attach_id = $staff->id;
        $user->attach_type = 'Staff';
        $this->parseInput($user,'confirmed');
        if(!$user->save()){
            $this->errors()->merge($user->errors());
            return false;
        }

        return true;
    }

    public function updateStaff($staff){

        $this->parseInput($staff,'name');



        if(!$staff->validate()){
            $this->errors()->merge($staff->errors());
            return false;
        }
        $oldUser = clone $staff->user;

        $this->parseInput($staff->user,'username');
        $this->parseInput($staff->user,'email');
        $this->parseInput($staff->user,'confirmed');
        $this->parseInput($staff->user,'password',$oldUser->password);
        $this->parseInput($staff->user,'password_confirmation',$oldUser->password_confirmation);
        $this->parseInput($staff->user,'confirmed');

        //add roles
        $staff->user->saveRoles(Input::get( 'roles' ));
        //update user
        if(!$staff->user->save()){
            $this->errors()->merge($staff->user->errors());
            return false;
        }

        //update staff
        if(!$staff->save()){
            $this->errors()->merge($staff->errors());
            return false;
        }

        return true;
    }

    public function deleteStaff($staff){
        if(!$staff->delete()){
            $this->errors()->merge($staff->errors());
            return false;
        }
        return true;
    }

    public function modifyPassword($staff){

        $repo = App::make('UserRepository');

        $input = Input::all();

        if (!$repo->login($input)) {
            $this->errors()->add('error', '旧密码有误！');
            return false;
        }

        $password = Input::get( 'new_password' );
        $passwordConfirmation = Input::get( 'new_password_confirmation' );

        if(!empty($password)) {
            if($password === $passwordConfirmation) {
                $staff->user->password = $password;
                $staff->user->password_confirmation = $passwordConfirmation;

                if ($staff->user->save()) {
                    return true;
                } else {
                    $this->errors()->merge($staff->user->errors());
                    return false;
                }
            }
            else {
                // Redirect to the new user page
                $this->errors()->add('error', Lang::get('admin/users/messages.password_does_not_match'));
                return false;
            }
        }
        else {
            unset($staff->user->password);
            unset($staff->user->password_confirmation);
        }
    }


}

